import 'package:flutter/material.dart';

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("命名路由"),
      ),
      body: FirstPage(),
    );
  }
}

//无状态页面 无法处理 从二级页面传回来的参数

class Homepage extends StatelessWidget {
  const Homepage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          ElevatedButton(
              onPressed: () {
                Navigator.pushNamed(context, 'product');
              },
              child: Text("跳转去商品界面")),
          ElevatedButton(
              onPressed: () {
                Navigator.pushNamed(context, 'user');
              },
              child: Text('跳转去用户界面')),
          ElevatedButton(
              onPressed: () {
                Navigator.pushNamed(context, 'productdetail',
                    arguments: {'title': '商品详情1'});
              },
              child: Text('跳转去商品详情1界面')),
          ElevatedButton(
              onPressed: () {
                Navigator.pushNamed(context, 'productdetail',
                    arguments: {'title': '商品详情2'});
              },
              child: Text('跳转去商品详情2界面')),
          ElevatedButton(
              onPressed: () {
                Navigator.pushNamed(context, 'productdetail',
                    arguments: {'title': '商品详情3'});
              },
              child: Text('跳转去商品详情3界面,收到回来的参数')),
        ],
      ),
    );
  }
}

//接受从二级页面返回的参数
class FirstPage extends StatefulWidget {
  FirstPage({Key? key}) : super(key: key);

  @override
  State<FirstPage> createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
  String _msg = '';
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          ElevatedButton(
              onPressed: () {
                Navigator.pushNamed(context, 'productdetail',
                    arguments: {'title': '商品详情3'}).then((value) => setState(()=> _msg = value as String) );
              },
              child: Text('跳转去商品详情3界面,收到回来的参数' + _msg)),
        ],
      ),
    );
  }
}

class Product extends StatelessWidget {
  const Product({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("商品页面"),
      ),
      body: Center(
        child: ElevatedButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child: Text("返回")),
      ),
    );
  }
}



class ProductDetail extends StatelessWidget {
  const ProductDetail({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final Map argument = ModalRoute.of(context)!.settings.arguments as Map;

    print(argument);

    return Scaffold(
      appBar: AppBar(
        title: Text("商品页面"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(argument['title']),
            ElevatedButton(
              //Hello 为当界面关闭时 从第二个界面传到上级页面的传参形式
                onPressed: () {
                  Navigator.pop(context, 'Hello');
                },
                child: Text("返回")),
          ],
        ),
      ),
    );
  }
}

class UnknowPage extends StatelessWidget {
  const UnknowPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("404"),
      ),
      body: Center(
        child: Text('404界面'),
      ),
    );
  }
}
